package oldking.wang;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;

public class LogicMain
{
    private String genCode;
    private int mIndex;

    public String Run(Sheet _sheet, String _codeTemplete)
    {
        genCode = "";
        mIndex = 0;

        //从三行开始
        //第一行 Row Index=0 用于定义Key
        //第二行 Row Index=1 用于定义Arg
        //第三行 Row Index=2 正式的数据
        int mCurrentYIndex = 2;

        // ==============
        // ==读取整个表格
        // ==============
        while (true)
        {
            if (_sheet.getRow(mCurrentYIndex) == null)
            {
                break;
            }

            //判断是否前置#,前置#则当前行不导出
            Cell ignoreCell = _sheet.getRow(mCurrentYIndex).getCell(0);
            if (ignoreCell != null && ignoreCell.getStringCellValue().startsWith("#"))
            {
                mCurrentYIndex++;
                continue;
            }
            Cell nameCell = _sheet.getRow(mCurrentYIndex).getCell(1);
            if (nameCell == null || nameCell.getStringCellValue().isEmpty())
            {
                mCurrentYIndex++;
                continue;
            }

            String name = nameCell.getStringCellValue();
            DoWriteGenCode(name);
            mCurrentYIndex++;
        }

        String newCode = _codeTemplete.replace("$a", genCode);
        return newCode;
    }

    private void DoWriteGenCode(String _name)
    {
        genCode += String.format("        public const string %s = \"%s\";\n", _name, "a" + mIndex);
        mIndex++;

    }
}
